// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.dcel.tools;

import java.util.HashMap;
import java.util.Map;

import com.dustedpixels.dcel.Instance;
import com.dustedpixels.dcel.StepInstance;
import com.dustedpixels.dcel.Unit;
import com.dustedpixels.dcel.Unit.Pin;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
final class StepInstanceOverInstance implements StepInstance {

  final Instance instance;
  final Map<String, int[]> pins;

  public StepInstanceOverInstance(Instance instance) {
    this.instance = instance;
    this.pins = new HashMap<String, int[]>();
    for (Pin pin : instance.getUnit().getPins()) {
      int[] data = new int[1];
      instance.setPinData(pin.getName(), data);
      pins.put(pin.getName(), data);
    }
  }
  
  public int getPin(String pinName) {
    checkPinName(pinName);
    return pins.get(pinName)[0];
  }

  public void setPin(String pinName, int value) {
    checkPinName(pinName);
    pins.get(pinName)[0] = value;
  }

  public Unit getUnit() {
    return instance.getUnit();
  }

  public void runCycle() {
    instance.run(1);
  }
  
  void checkPinName(String pinName) {
    if (!pins.containsKey(pinName)) {
      throw new IllegalArgumentException("No such pin: " + pinName);
    }
  }
}
